package Demo02.Demo02;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Demo_cover {
    public static void main(String[] args) {
        wordCase("G:\\学习\\txt.txt");
    }

    //测试方法：略...
    public static void wordCase(String path){
        StringBuilder sb = null;
        /*读取文件内容，并保存成字符串*/
        try(
                FileReader fr = new FileReader(path))
        {
            System.out.print(new File(path).length());
            //记录读取到的字符
            sb = new StringBuilder();
            int hasRead = 0;
            while((hasRead = fr.read()) != -1){
                char c = (char)hasRead;
                //转空格
                if(c == ' '){
                    sb.append("_");
                }
                System.out.print(c);
                //判断是否是小写
                if(Character.isLowerCase(c)){
                    //转换成大写并加入字符串
                    sb.append(Character.toUpperCase(c));
                }else{
                    //转换成小写并加入字符串
                    sb.append(Character.toLowerCase(c));
                }
            }
        }catch(IOException e){
            e.printStackTrace();
        }
        /*将新字符串内容写入文件*/
        try(
                FileWriter fw = new FileWriter(path))
        {
            fw.write(sb.toString());
        }catch(IOException e){
            e.printStackTrace();
        }


    }
}
